package edu.wpi.cs509.ui;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.*;

import edu.wpi.cs509.ui.menu.StrategoMenu;

/**
 * The StrategoClient class implements the main application from for
 * the client.  
 * 
 * @author afoltan
 *
 */
@SuppressWarnings("serial")
public class StrategoClientFrame extends JFrame implements WindowListener {
	
	// the top level JPanel which is switched between the Login and the GamesList
	// based on state
	private JPanel mainPanel;
	
	private LoginScreenPanel loginPanel;
	private GameListPanel gameListPanel;
	
	// the main application menu
	private StrategoMenu mainMenu;

	/**
	 * Constructor for the client application
	 */
	public StrategoClientFrame( A__MainStrategoClient client ) {
		
		// Create the main menu bar
		mainMenu = new StrategoMenu(client);
		setJMenuBar(mainMenu);
		
		// login panel
		loginPanel = new LoginScreenPanel(client);
		
		// game list panel
		gameListPanel = new GameListPanel(client);
		
		// catch and confirm window close
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		addWindowListener( this );
		
		// set the initial panel to the login
		displayLoginPanel();
	}
	
	public void displayLoginPanel() {
		
		try {
			remove( mainPanel );
		} catch (NullPointerException e) {
			//ignore
		}

		// set the main panel to the login
		mainPanel = loginPanel;
		add(mainPanel);
		
		mainPanel.setVisible(true);
	}

	public void displayGameListPanel() {
		
		try {
			remove( mainPanel );
		} catch (NullPointerException e) {
			//ignore
		}

		// set the main panel to the login
		mainPanel = gameListPanel;
		add(mainPanel);
		
		mainMenu.addGameMenus();
		mainPanel.setVisible(true);
	}
	
	public GameListPanel getGameListPanel() {
		return gameListPanel;
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		int ans = Dialog.displayQuitConfirmDialog();
		if (ans == 0) {
			this.dispose();
		}
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
